﻿using MvvmCross.Core.ViewModels;
using Sidvall.WorkItemManager.Data;
using Sidvall.WorkItemManager.Workflow.WorkItems;
using System.Windows.Input;

namespace Sidvall.WorkItemManager.UI.ViewModels.WorkItems
{
    public class ConsoleWorkItemContextViewModel : WorkItemContextViewModel
    {
        #region Public Members

        #region ContextItem

        private ConsoleWorkItemContext _ContextItem;
        public ConsoleWorkItemContext ContextItem
        {
            get
            {
                return _ContextItem;
            }
            set
            {
                if (_ContextItem != null)
                {
                    _ContextItem.PropertyChanged -= OnContextItemPropertyChanged;
                    _ContextItem.ChildChanged -= OnContextItemChildChanged;
                }
                _ContextItem = value;
                if (_ContextItem != null)
                {
                    _ContextItem.PropertyChanged += OnContextItemPropertyChanged;
                    _ContextItem.ChildChanged += OnContextItemChildChanged;
                }
                RaisePropertyChanged(nameof(ContextItem));
                OnWorkItemContextChanged();
            }
        }

        #endregion
        #region WindowStyles

        private Sidvall.Data.KeyValueCollection<string, Sidvall.SystemEnvironment.ViewSizePreference> _WindowStyles;
        public Sidvall.Data.KeyValueCollection<string, Sidvall.SystemEnvironment.ViewSizePreference> WindowStyles
        {
            get
            {
                if (_WindowStyles == null)
                    this.WindowStyles = Sidvall.Data.KeyValueManager.FromEnum<Sidvall.SystemEnvironment.ViewSizePreference>();
                return _WindowStyles;
            }
            set
            {
                _WindowStyles = value;
                RaisePropertyChanged(nameof(WindowStyles));
            }
        }

        #endregion
        #region ListIsVisible

        private bool _ListIsVisible = true;
        public bool ListIsVisible
        {
            get
            {
                return _ListIsVisible;
            }
            set
            {
                if (_ListIsVisible == value)
                    return;
                _ListIsVisible = value;
                RaisePropertyChanged(nameof(ListIsVisible));
            }
        }

        #endregion
        #region ActiveSensor

        private Sensor _ActiveSensor;
        public Sensor ActiveSensor
        {
            get
            {
                return _ActiveSensor;
            }
            set
            {
                _ActiveSensor = value;
                RaisePropertyChanged(nameof(ActiveSensor));
            }
        }

        #endregion

        // Commands
        #region AddSensorCommand

        public ICommand AddSensorCommand
        {
            get
            {
                return new MvxCommand(AddSensor);
            }
        }
        public void AddSensor()
        {
            if (this.ContextItem == null)
                return;
            var item = new Sensor()
            {
            };
            this.ContextItem.Sensors.Add(item);
            this.ActiveSensor = item;
            this.ListIsVisible = false;
        }

        #endregion
        #region DeleteSensorCommand

        public ICommand DeleteSensorCommand
        {
            get
            {
                return new MvxCommand<Sensor>(DeleteSensor);
            }
        }
        public void DeleteSensor(Sensor item)
        {
            if (this.ContextItem == null)
                return;
            if (item == null)
                return;
            this.ContextItem.Sensors.Remove(item);
        }

        #endregion
        #region EditSensorCommand

        public ICommand EditSensorCommand
        {
            get
            {
                return new MvxCommand<Sensor>(EditSensor);
            }
        }
        public void EditSensor(Sensor item)
        {
            if (this.ContextItem == null)
                return;
            if (item == null)
                return;
            this.ActiveSensor = item;
            this.ListIsVisible = false;
        }

        #endregion
        #region ShowSensorListCommand

        public ICommand ShowSensorListCommand
        {
            get
            {
                return new MvxCommand(ShowSensorList);
            }
        }
        public void ShowSensorList()
        {
            this.ListIsVisible = true;
        }

        #endregion

        #region OnWorkItemContextChanged

        protected void OnWorkItemContextChanged()
        {
            this.Context = ConsoleWorkItemContext.Serialize(this.ContextItem);
        }

        #endregion
        #region OnContextItemChildChanged

        private void OnContextItemChildChanged(object sender, Sidvall.Business.ChildChangedEventArgs e)
        {
            this.Context = ConsoleWorkItemContext.Serialize(this.ContextItem);
        }

        #endregion
        #region OnContextItemPropertyChanged

        private void OnContextItemPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            this.Context = ConsoleWorkItemContext.Serialize(this.ContextItem);
        }

        #endregion

        #region InitializeContext

        public override void InitializeContext()
        {
            this.ContextItem = ConsoleWorkItemContext.Deserialize(this.Context);
        }

        #endregion

        #endregion
    }
}
